
// 乘法
export function mul(arg1, arg2) {
  const r1 = arg1.toString()
  const r2 = arg2.toString()
  let m
  let resultVal
  const d = arguments[2]
  m = (r1.split('.')[1] ? r1.split('.')[1].length : 0) + (r2.split('.')[1] ? r2.split('.')[1].length : 0)
  resultVal = (Number(r1.replace('.', '')) * Number(r2.replace('.', ''))) / Math.pow(10, m)
  return typeof d !== 'number' ? Number(resultVal) : Number(resultVal.toFixed(parseInt(d)))
}

export function div(arg1, arg2) {
  const r1 = arg1.toString()
  const r2 = arg2.toString()
  let m
  let resultVal
  const d = arguments[2] || 2
  m = (r2.split('.')[1] ? r2.split('.')[1].length : 0) - (r1.split('.')[1] ? r1.split('.')[1].length : 0)
  resultVal = (Number(r1.replace('.', '')) / Number(r2.replace('.', ''))) * Math.pow(10, m)
  return typeof d !== 'number' ? Number(resultVal) : Number(resultVal.toFixed(parseInt(d)))
}

export function add(arg1, arg2) {
  arg1 = arg1.toString()
  arg2 = arg2.toString()
  const arg1Arr = arg1.split('.')
  const arg2Arr = arg2.split('.')
  const d1 = arg1Arr.length === 2 ? arg1Arr[1] : ''
  const d2 = arg2Arr.length === 2 ? arg2Arr[1] : ''
  const maxLen = Math.max(d1.length, d2.length)
  const m = Math.pow(10, maxLen)
  const result = Number(((arg1 * m + arg2 * m) / m).toFixed(maxLen))
  const d = arguments[2]
  return typeof d === 'number' ? Number(result.toFixed(d)) : result
}

export function sub(arg1, arg2) {
  return this.add(arg1, -Number(arg2), arguments[2])
}

/**
 * 防抖
 * @param fn
 * @param time
 * @returns {(function(): void)|*}
 */
export function debounce(fn, time) {
  let _arguments = arguments
  let timeout = null
  return function() {
    if (timeout) {
      clearTimeout(timeout)
    }
    timeout = setTimeout(() => {
      fn.call(this, _arguments)
    }, time)
  }
}

/**
 * 动态获取图片资源
 * @param url 图片名称（包含图片后缀）
 * @returns {any} 返回图片路径
 */
export function getAssetsFile  (url){
  // 获取文件夹下所有图片名称
  let files = import.meta.globEager('../assets/**/*.png');
  let AllPics = Object.keys(files).map((v) => v);
  let isExist = 0;
  isExist = AllPics.find((item) => {
    return item.includes(url)
  })
  return files[isExist].default
}

/**
  下载后台返回blob文件
  @params data：后台返回二进制
  @params name：文件名，默认命名“文件-下载年月日”
  @params fileType 下载文件类型
  */
export function downLoad(data, name, fileType) {
  let blob = new Blob([data], {
    type: 'application/pdf;chartset=UTF-8'
  })
  let link = document.createElement('a')
  link.download = `${name + fileType}`
  link.target = '_blank'
  link.href = URL.createObjectURL(blob)
  link.click()
}
